package org.janusproject.demos.acl.cnp.agent;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.janusproject.acl.ACLAgent;
import org.janusproject.acl.protocol.AbstractFipaProtocol;
import org.janusproject.acl.protocol.EnumFipaProtocol;
import org.janusproject.acl.protocol.FipaConversationManager;
import org.janusproject.demos.acl.cnp.organization.ContractNetOrganization;
import org.janusproject.demos.acl.cnp.role.ContractNetAnswerer;
import org.janusproject.kernel.crio.core.GroupAddress;
import org.janusproject.kernel.status.Status;
import org.janusproject.kernel.status.StatusFactory;

public class ACLCallForProposalReceiver extends ACLAgent {

	private static final long serialVersionUID = -7078376398776599011L;
	
	protected FipaConversationManager protocolManager;

	@Override
	public Status activate(Object... parameters) {
		protocolManager = new FipaConversationManager(this);
		AbstractFipaProtocol protocol = protocolManager.createConversation(EnumFipaProtocol.FIPA_CONTRACT_NET, "First contract net protocol");
		protocol.initiateAsParticipant();
		
		Logger.getAnonymousLogger().log(Level.INFO, "receiver protocol created");
		
		GroupAddress groupAddress = getOrCreateGroup(ContractNetOrganization.class);	
		
		if ( !requestRole( ContractNetAnswerer.class, groupAddress, protocol ) ) {
			return StatusFactory.cancel(this);
		}
		
		return StatusFactory.ok(this);
	}	
}